﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace GameJam2012 {
   public class ParticleSystem {

        private const int NUM_OF_PARTICLES = 50;
        private const int PARTICLE_STRENGTH = 5;
        private const float PARTICLE_SIZE = 0.3f;
        private const int CAP_LIST_SIZE = 20;
        private Vector2 gravity;
        private Random random;
        private Texture2D particle;
        private List<List<Particle>> groupOfParticles;

        public ParticleSystem() {
            particle = SnakeGame.PARTICLE;
            random = new Random();
            gravity = new Vector2(0.0f, 0.5f);
            groupOfParticles = new List<List<Particle>>();

        }

        public void Update() {
            int totalGroups = groupOfParticles.Count;
            for (int i = 0; i < totalGroups; ++i) {
                int lengthOfGroup = groupOfParticles[i].Count;
                for (int j = 0; j < lengthOfGroup; ++j) {
                    groupOfParticles[i][j].Update();
                    if (!groupOfParticles[i][j].IsAlive()) {
                        groupOfParticles[i].RemoveAt(j--);
                        lengthOfGroup--;
                    }
                }
                if (groupOfParticles[i].Count == 0) {
                    groupOfParticles.RemoveAt(i--);
                    totalGroups--;
                }
                
            }
        }
                        

        public void Draw(SpriteBatch spriteBatch, Vector2 offset) {
            int totalGroups = groupOfParticles.Count;
            for (int i = 0; i < totalGroups; ++i) {
                int lengthOfGroup = groupOfParticles[i].Count;
                for (int j = 0; j < lengthOfGroup; ++j) {
                    groupOfParticles[i][j].Draw(spriteBatch, particle,offset);
                }
            }
        }

        public void Explode(Vector2 position) {
            List<Particle> particles = new List<Particle>();
            for (int i = 0; i < NUM_OF_PARTICLES; ++i) {
                float angle = (float)(random.NextDouble() * MathHelper.TwoPi);
                Vector2 velocity = new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));
                velocity *= (float)(random.NextDouble() * PARTICLE_STRENGTH);
                particles.Add(new Particle(position, velocity, Color.Pink, false, PARTICLE_SIZE));
            }
            if(groupOfParticles.Count <= CAP_LIST_SIZE/4) {
                groupOfParticles.Add(particles);
            }
        }

        public void Gush(Vector2 position) {
            List<Particle> particles = new List<Particle>();
            for (int i = 0; i < NUM_OF_PARTICLES; ++i) {
                float angle = (float)(random.NextDouble() * MathHelper.TwoPi);
                Vector2 velocity = new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));
                velocity *= (float)(random.NextDouble() * PARTICLE_STRENGTH);
                particles.Add(new Particle(position, velocity, Color.Maroon, true, PARTICLE_SIZE));
            }
            if (groupOfParticles.Count <= CAP_LIST_SIZE) {
                groupOfParticles.Add(particles);
            }
        }

        //public void Party(Vector2 position) {
        //    List<Particle> particles = new List<Particle>();
        //    for (int i = 0; i < NUM_OF_PARTICLES; i++) {
        //    }
        //}

        public void LavaGush(Vector2 position) {
            List<Particle> particles = new List<Particle>();
            List<Particle> particles2 = new List<Particle>();
            for (int i = 0; i < NUM_OF_PARTICLES; ++i) {

                float rndX = 1.0f * ((float)random.NextDouble() - 0.4f);
                float rndY = -1 - 1 * (float)random.NextDouble();
                Vector2 velocity = new Vector2 (rndX,rndY);
                velocity *= (float)(random.NextDouble() * PARTICLE_STRENGTH * 2.5f);
                particles.Add(new Particle(position, velocity, Color.Orange, true, PARTICLE_SIZE));



                float rndX2 = 1.0f * ((float)random.NextDouble() - 0.4f);
                float rndY2 = -1 - 1 * (float)random.NextDouble();
                Vector2 velocity2 = new Vector2(rndX2, rndY2);
                velocity2 *= (float)(random.NextDouble() * PARTICLE_STRENGTH * 2.5f);
                particles2.Add(new Particle(position, velocity2, Color.DarkRed, true, PARTICLE_SIZE));
                
            }
            if (groupOfParticles.Count <= CAP_LIST_SIZE/2) {
                groupOfParticles.Add(particles);
                groupOfParticles.Add(particles2);
            }

        }
       
    }
}
